import java.util.ArrayList;


public class Board {

	private static int[][] costmat 
			={
					{50,-1,5,2,2,5,-1,50},
					{-1,-10,1,1,1,1,-10,-1},
					{5,1,1,1,1,1,1,5},
					{2,1,1,0,0,1,1,2},
					{2,1,1,0,0,1,1,2},
					{5,1,1,1,1,1,1,5},
					{-1,-10,1,1,1,1,-10,-1},
					{50,-1,5,2,2,5,-1,50},
			
			};


	
	private int[][] board;
	private int heuristic;	
	
	
	/**
	 * new Board
	 * 
	 * '2' is black
	 * '1' is white
	 * 
	 */
	public Board(){
		board = new int[8][8];
		heuristic = 0;
		
	}
	
	public void initiate(){
		board[3][3] = 1;
		board[3][4] = 2;	
		board[4][3] =2;	
		board[4][4] = 1;
		board[4][4] = 1;
	}
	
	public int[][] getData(){
		return board;
	}

	public void set(int x, int y, int color){
		board[x][y] = color;
	}
	
	
	
	public ArrayList<Coordinate> getPossibleMoves(int color){
		ArrayList<Coordinate> possible = new  ArrayList<Coordinate>();
		
		for (int i = 0; i < board.length; i++) {
			for (int j = 0; j < board.length; j++) {
				
				if (board[j][i] == color){
					for (Coordinate coordinate :getPossibleMovesFrom(j, i, color)) {
						if (!possible.contains(coordinate))
							possible.add(coordinate);
						}
					}
				
				}
			}
		
		
		
		return possible;
	}
	
	/**
	 * calculates all possible moves that are made possible from this position
	 * 
	 * @param x
	 * 	|X_Coordinate of the board where to start checking  
	 * @param y
	 * 	|Y coordinate of the board where to start checking
	 * @param color
	 * 	| the color to check
	 * @return
	 * 	| an arraylist of coordinates where a piece can be placed;
	 */
	private ArrayList<Coordinate> getPossibleMovesFrom(int x, int y, int color){
		ArrayList<Coordinate> possible = new  ArrayList<Coordinate>();
		
		Coordinate co;
		co = getPossibleMovesFrom(x,y,color,1,0);
		if (co != null)
			possible.add(co);
		
		co = getPossibleMovesFrom(x,y,color,0,1);
		if (co != null)
			possible.add(co);
		
		co = getPossibleMovesFrom(x,y,color,1,1);
		if (co != null)
			possible.add(co);
		
		co = getPossibleMovesFrom(x,y,color,-1,0);
		if (co != null)
			possible.add(co);
		
		co = getPossibleMovesFrom(x,y,color,0,-1);
		if (co != null)
			possible.add(co);
		
		co = getPossibleMovesFrom(x,y,color,-1,-1);
		if (co != null)
			possible.add(co);
		
		co = getPossibleMovesFrom(x,y,color,-1,1);
		if (co != null)
			possible.add(co);
		
		co = getPossibleMovesFrom(x,y,color,1,-1);
		if (co != null)
			possible.add(co);
		
		
		
		
		
		return possible;
	}
	
	
	/**
	 * check from a given coordinate if there is a possible move in a specified direction 
	 * @param x
	 * @param y
	 * @param color
	 * @param xdir
	 * 	|direction, -1 or 1 
	 * @param ydir
	 * 	|direction, -1 or 1
	 * @return
	 */
	private Coordinate getPossibleMovesFrom(int x, int y, int color, int xdir, int ydir){
		
		try{
			if (nextIsNotOppositeColor(x, y, xdir, ydir) )
				return null;
		}catch(Exception e){
			return null; 
		}
		
		int i = 2;
		
		// keep going further untill an empty space is found -> possible move or an out of bounds -> no free spot or ur own color -> no free spot
		while (true){
			try{
				
				if (board[x+(i*xdir)][y+(i*ydir)] == board[x][y])
					return null;
				
				if (board[x+(i*xdir)][y+(i*ydir)]==0)
					return new Coordinate(x+(i*xdir),y+(i*ydir));
				
				i++;
				
			}catch(Exception e){
				return null;
			}
		}
		
		
		
	}

	
	
	private boolean nextIsNotOppositeColor(int x, int y, int xdir, int ydir) {
		return board[x+xdir][y+ydir]== 0 || board[x+xdir][y+ydir] == board[x][y];
	}
	
	
	public void drawBoard(){
		for (int i = 0; i < board.length; i++) {
			String s = "";
			for (int j = 0; j < board.length; j++) {
				s+= board[j][i] + " ";
			}
			System.out.println(s);
		}
		
	}	
		
	public void setPiece(int x, int y, int color){
		set(x,y,color);
		ArrayList<Coordinate> possible = new  ArrayList<Coordinate>();
		
		ArrayList<Coordinate> co = new ArrayList<Coordinate>();
		co = getPiecesToFlip(x,y,color,1,0);
		if (co != null)
			possible.addAll(co);
		
		co = getPiecesToFlip(x,y,color,0,1);
		if (co != null)
			possible.addAll(co);
		
		co = getPiecesToFlip(x,y,color,1,1);
		if (co != null)
			possible.addAll(co);
		
		co = getPiecesToFlip(x,y,color,-1,0);
		if (co != null)
			possible.addAll(co);
		
		co = getPiecesToFlip(x,y,color,0,-1);
		if (co != null)
			possible.addAll(co);
		
		co = getPiecesToFlip(x,y,color,-1,-1);
		if (co != null)
			possible.addAll(co);
		
		co = getPiecesToFlip(x,y,color,-1,1);
		if (co != null)
			possible.addAll(co);
		
		co = getPiecesToFlip(x,y,color,1,-1);
		if (co != null)
			possible.addAll(co);
		
		for (Coordinate coordinate : possible) {
			set(coordinate.x,coordinate.y,(color==1)?1:2);
			//System.out.println("draw: " + ((color==1)?1:2) );
		}
		
		
	}
		
		
public ArrayList<Coordinate> getPiecesToFlip(int x, int y, int color, int xdir, int ydir){
	try{
		if (nextIsNotOppositeColor(x, y, xdir, ydir) )
			return null;
	}catch(Exception e){
		return null; 
	}
	
	
		ArrayList<Coordinate> toflip = new ArrayList<Coordinate>();
		int i = 1;
		while (true){
			try{
				
				if (board[x+(i*xdir)][y+(i*ydir)] == board[x][y])
					return toflip;
				
			toflip.add(	new Coordinate(x+(i*xdir),y+(i*ydir)));	
				
				
				i++;
				
			}catch(Exception e){
				return null;
			}
			
		}
		
		
		
		
	}
		
		
		
		
		
		
		
	
	
	public static void main(String args[]){
		Board b = new Board();
		b.initiate();
		
		b.set(4,5,2);
		b.drawBoard();
		//b.set(5,4,2);
		//b.set(6,4,2);
		//b.set(5,5,2);
		
		
	/*for (Coordinate co :b.getPiecesToFlip(4,5,2,0,-1)) {
		System.out.println(co.toString() + ",");
	}*/	
		b.setPiece(4, 5, 2);
		b.drawBoard();
		
		
	}
	
}
